import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreSiteAccountService extends BaseService<any> {
  private readonly logger = new Logger(CoreSiteAccountService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * addPayLog
   * 对应 PHP: CoreSiteAccountService_core::addPayLog()
   * 逻辑类型: undefined - undefined
   */
  async addPayLog(pay_info: any[]) {
    // 基于PHP真实逻辑: addPayLog
    // PHP原文: $pay_info = ( new Pay() )->where([ [ 'out_trade_no', '=', $pay_info[ 'out_trade_no' ] ], [ 'site_id', '=', $pay_info[ 'site_id' ] ] ])->findOrEmpty()-...
const pay_info = ( this.payService ).where([ [ 'out_trade_no', '=', pay_info[ 'out_trade_no' ] ], [ 'site_id', '=', pay_info[ 'site_id' ] ) )).findOrEmpty().toArray(];
        const data = [
            site_id: pay_info[ 'site_id' ],
            type: 'pay',
            money: pay_info[ 'money' ],
            trade_no: pay_info[ 'out_trade_no' ],
        );
        const res = ( this.siteAccountLogService ).create(data);
        return res.id;
    }

    /**
     * 添加退款账单记录
     * @param int site_id
     * @param string refund_no
     * @return mixed
     */
    async addRefundLog(int site_id, string refund_no)
    {
        const refund_info = ( this.refundService ).where([ [ 'refund_no', '=', refund_no ], [ 'site_id', '=', site_id ) )).findOrEmpty().toArray(];
        const data = [
            site_id: refund_info[ 'site_id' ],
            type: 'refund',
            money: refund_info[ 'money' ) * ( -1 ),
            trade_no: refund_info[ 'refund_no' ],
        ];
        const res = ( this.siteAccountLogService ).create(data);
        return res.id;
    }

    /**
     * 添加转账账单记录
     * @param int site_id
     * @param string transfer_no
     * @return mixed
     */
    async addTransferLog(int site_id, string transfer_no)
    {
        const transfer_info = ( this.transferService ).where([ [ 'transfer_no', '=', transfer_no ], [ 'site_id', '=', site_id ) )).findOrEmpty().toArray(];
        const data = [
            site_id: transfer_info[ 'site_id' ],
            type: 'transfer',
            money: transfer_info[ 'money' ) * ( -1 ),
            trade_no: transfer_info[ 'transfer_no' ],
        ];
        const res = ( this.siteAccountLogService ).create(data);
        return res.id;
    }

}
  }

  /**
   * addRefundLog
   * 对应 PHP: CoreSiteAccountService_core::addRefundLog()
   * 逻辑类型: undefined - undefined
   */
  async addRefundLog(site_id: number, refund_no: number) {
    // 基于PHP真实逻辑: addRefundLog
    // PHP原文: $refund_info = ( new Refund() )->where([ [ 'refund_no', '=', $refund_no ], [ 'site_id', '=', $site_id ] ])->findOrEmpty()->toArray();         $data = ...
const refund_info = ( this.refundService ).where([ [ 'refund_no', '=', refund_no ], [ 'site_id', '=', site_id ) )).findOrEmpty().toArray(];
        const data = [
            site_id: refund_info[ 'site_id' ],
            type: 'refund',
            money: refund_info[ 'money' ) * ( -1 ),
            trade_no: refund_info[ 'refund_no' ],
        ];
        const res = ( this.siteAccountLogService ).create(data);
        return res.id;
    }

    /**
     * 添加转账账单记录
     * @param int site_id
     * @param string transfer_no
     * @return mixed
     */
    async addTransferLog(int site_id, string transfer_no)
    {
        const transfer_info = ( this.transferService ).where([ [ 'transfer_no', '=', transfer_no ], [ 'site_id', '=', site_id ) )).findOrEmpty().toArray(];
        const data = [
            site_id: transfer_info[ 'site_id' ],
            type: 'transfer',
            money: transfer_info[ 'money' ) * ( -1 ),
            trade_no: transfer_info[ 'transfer_no' ],
        ];
        const res = ( this.siteAccountLogService ).create(data);
        return res.id;
    }

}
  }

  /**
   * addTransferLog
   * 对应 PHP: CoreSiteAccountService_core::addTransferLog()
   * 逻辑类型: undefined - undefined
   */
  async addTransferLog(site_id: number, transfer_no: number) {
    // 基于PHP真实逻辑: addTransferLog
    // PHP原文: $transfer_info = ( new Transfer() )->where([ [ 'transfer_no', '=', $transfer_no ], [ 'site_id', '=', $site_id ] ])->findOrEmpty()->toArray();         ...
const transfer_info = ( this.transferService ).where([ [ 'transfer_no', '=', transfer_no ], [ 'site_id', '=', site_id ) )).findOrEmpty().toArray(];
        const data = [
            site_id: transfer_info[ 'site_id' ],
            type: 'transfer',
            money: transfer_info[ 'money' ) * ( -1 ),
            trade_no: transfer_info[ 'transfer_no' ],
        ];
        const res = ( this.siteAccountLogService ).create(data);
        return res.id;
    }

}
  }
}
